﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using control.Dal;
using System.Collections;

namespace control.Biz
{
    public class BizTonKho
    {
        public int ID { get; set; }
        public int IDSach { get; set; }
        public DateTime ThoiGian { get; set; }
        public int SoLuong { get; set; }

        internal static int GetTonKhoOfSach(int Input)
        {
            return Convert.ToInt32(DalPhieuXuatHang.GetTonKhoOfSach(Input));
        }

        internal void Insert()
        {
            ID= DalTonKho.Insert(this);
        }
        private BizSach Sach
        {
            get { return BizSach.GetSachByID(IDSach); }
        }
        public string TenSach
        {
            get { return Sach.TenSach; }
        }
        public string MaSach
        {
            get { return Sach.MaSach; }
        }
        public static List<BizTonKho> Find(string Sach, DateTime fromDate, DateTime toDate)
        {
            List<BizTonKho> resuilt = new List<BizTonKho>();
            DateTime newFrom = new DateTime(fromDate.Year, fromDate.Month, fromDate.Day, 0, 0, 1);
            DateTime newTo = new DateTime(toDate.Year, toDate.Month, toDate.Day, 23, 59, 59);
            List<BizTonKho> tonkhos = DalTonKho.Find(Sach, newFrom, newTo);
            var Group = tonkhos.GroupBy(t => t.ThoiGian.ToShortDateString());
            foreach (var item in Group)
            {
                BizTonKho tonkho = new BizTonKho();

                foreach (var it in item)
                {
                    if (it.ThoiGian.CompareTo(tonkho.ThoiGian) > 0)
                    {
                        tonkho = new BizTonKho()
                        {
                            ID = it.ID,
                            IDSach = it.IDSach,
                            SoLuong = it.SoLuong,
                            ThoiGian = it.ThoiGian
                        };
                    }
                }
                resuilt.Add(tonkho);
            }
            return resuilt;                  
        }
    }
}
